package com.poetry.vup.user.service.impl;

import com.poetry.vup.common.entity.user.VupUserInfoDO;
import com.poetry.vup.common.exception.KabobExceptionUtil;
import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.ResultCode;
import com.poetry.vup.common.util.JacksonUtil;
import com.poetry.vup.common.util.OSSClientUtil;
import com.poetry.vup.user.domain.VupUserInfoDTO;
import com.poetry.vup.user.domain.query.Persona;
import com.poetry.vup.user.mapper.VupUserInfoMapper;
import com.poetry.vup.user.service.VupUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
@Transactional
public class VupUserInfoServiceImpl implements VupUserInfoService {

    @Autowired
    private VupUserInfoMapper vupUserInfoMapper;

    @Autowired
    private OSSClientUtil ossClientUtil;

    /**
     * 查找用户详情信息
     * @param userId
     * @return
     */
    @Override
    public BaseResult getUserInfo(Long userId) {
        BaseResult result = BaseResult.failed("");
        VupUserInfoDO vupUserInfoDO = vupUserInfoMapper.selectById(userId);
        return BaseResult.success(vupUserInfoDO);
    }

    @Override
    public VupUserInfoDO findById(Long id) {
        return vupUserInfoMapper.selectById(id);
    }

    /**
     * 修改个人信息
     * @param vupUserInfoDTO
     * @return
     */
    @Override
    public int update(VupUserInfoDTO vupUserInfoDTO) {
        int id = vupUserInfoMapper.updateById(JacksonUtil.objectToObject(vupUserInfoDTO, VupUserInfoDO.class));
        return id;
    }

    /**
     * t添加个人信息
     * @param vupUserInfoDTO
     * @return
     */
    @Override
    public int addUserInfo(VupUserInfoDTO vupUserInfoDTO) {
        int insert = vupUserInfoMapper.insert(JacksonUtil.objectToObject(vupUserInfoDTO, VupUserInfoDO.class));
        return insert;
    }

    /**
     * 上传个人封面
     * @param userId
     * @param file
     * @return
     */
    @Override
    public Long uploadUserImage(Long userId, MultipartFile file) {
        try {
            String key = ossClientUtil.uploadImg2Oss(file);
            String url = ossClientUtil.getUrl(key);
            //更新到个人信息
            VupUserInfoDO vupUserInfoDO = vupUserInfoMapper.selectById(userId);
            if(vupUserInfoDO != null){
                vupUserInfoDO.setUserImage(url);
                int i = vupUserInfoMapper.updateById(vupUserInfoDO);
                if(i<0){
                    return -1L;
                }
            }
        } catch (Exception e) {
            KabobExceptionUtil.throwError(ResultCode.FAILED,"上传失败",e);
        }
        return 1L;
    }

    /**
     * 添加人设
     * @param persona
     * @return
     */
    @Override
    public Long addPersona(Persona persona) {
        VupUserInfoDO vupUserInfoDO = vupUserInfoMapper.selectById(persona.getUserId());
        if(vupUserInfoDO != null){
            vupUserInfoDO.setAddress(persona.getAddress());
            vupUserInfoDO.setFans(persona.getFans());
            vupUserInfoDO.setHeight(persona.getHeight());
            vupUserInfoDO.setWeight(persona.getWeight());
            vupUserInfoDO.setPersonalIntroduction(persona.getPersonalIntroduction());
            int updateById = vupUserInfoMapper.updateById(vupUserInfoDO);
            return Long.valueOf(updateById);
        }
        return 0L;
    }
}
